package com.cjack.frenchexcel.util;

import com.cjack.frenchexcel.exception.CommonException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.Collection;

/**
 * 断言工具类
 *
 * @author apple
 */
public class AssertUtil {

    public AssertUtil() {
    }

    public static void assertNotNull(Object obj, Integer errorCode) {
        if (obj == null) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertNotNull(Object obj, Integer errorCode, String errorMsg) {
        if (obj == null) {
            throw new CommonException(errorCode, errorMsg);
        }
    }

    public static void assertStrEmpty(String obj, Integer errorCode) {
        if (!StringUtils.isBlank(obj)) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertStrNotEmpty(String obj, Integer errorCode) {
        if (StringUtils.isBlank(obj)) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertStrNotEmpty(String obj, Integer errorCode, String errMsg) {
        if (StringUtils.isEmpty(obj)) {
            throw new CommonException(errorCode, errMsg);
        }
    }

    public static void assertNotEmpty(Collection obj, Integer errorCode, String errorMsg) {
        if (CollectionUtils.isEmpty(obj)) {
            throw new CommonException(errorCode, errorMsg);
        }
    }

    public static void assertEqual(int int1, int in2, Integer errorCode, String errorMsg, Object... args) {
        if (int1 != in2) {
            String msg = MessageFormat.format(errorMsg, args);
            throw new CommonException(errorCode, msg);
        }
    }

    public static void assertEqual(int int1, int in2, Integer errorCode) {
        if (int1 != in2) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertEqual(String obj1, String obj2, Integer errorCode, String errorMsg) {
        if (StringUtils.isNoneBlank(obj1) && StringUtils.isNoneBlank(obj2)) {
            if (!StringUtils.equals(obj1, obj2)) {
                throw new CommonException(errorCode, errorMsg);
            }
        }
    }

    public static void assertNotEqual(int int1, int in2, Integer errorCode) {
        if (int1 == in2) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertNotEqual(int int1, int in2, Integer errorCode, String errorMsg) {
        if (int1 == in2) {
            throw new CommonException(errorCode, errorMsg);
        }
    }

    public static void assertNotEqual(long int1, long in2, Integer errorCode, String errorMsg) {
        if (int1 == in2) {
            throw new CommonException(errorCode, errorMsg);
        }
    }

    public static void assertEqual(long int1, long in2, Integer errorCode, String errorMsg) {
        if (int1 != in2) {
            throw new CommonException(errorCode, errorMsg);
        }
    }


    public static void assertNull(Object obj, Integer errorCode) {
        if (obj != null) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertNull(Object obj, Integer errorCode, String errorMsg) {
        if (obj != null) {
            throw new CommonException(errorCode, errorMsg);
        }
    }

    public static void assertNotEqual(Integer obj1, Integer obj2, Integer errorCode) {
        if (obj1 != null && obj2 != null) {
            if (obj1.equals(obj2)) {
                throw new CommonException(errorCode);
            }
        }
    }

    public static void assertTrue(boolean condition, Integer errorCode) {
        if (!condition) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertTrue(boolean condition, Integer errorCode, String errorMsg) {
        if (!condition) {
            throw new CommonException(errorCode, errorMsg);
        }
    }

    public static void assertFalse(boolean condition, Integer errorCode) {
        if (condition) {
            throw new CommonException(errorCode);
        }
    }

    public static void assertFalse(boolean condition, Integer errorCode, String errorMsg) {
        if (condition) {
            throw new CommonException(errorCode, errorMsg);
        }
    }
}
